package com.ygqh.baby.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.MDC;

import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.utils.SessionUtil;

public class YgLog4jFilter implements Filter {
	private final static String DEFAULT_USERID = "系统";

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		YgUser ygUser = SessionUtil.getCurrentUser();
		if (ygUser == null) {
			MDC.put("user_name", DEFAULT_USERID);
		} else {
			MDC.put("user_name", ygUser.getUserName());
		}
		// logger.info("test for MDC.");

		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
